"ui";
ui.layout(
    <vertical padding="16">
        <text id="adroidId" text="红色字体" textColor="red"/>
        <button id="copy" text="复制"/>
        <input id="key"/>
        <radiogroup id="radio">
            <radio id="radio1" text="第一行"/>
            <radio id="radio2" text="第二行"/>
            <radio id="radio3" text="第三行"/>
            <radio id="radio4" text="三行一起" checked="true"/>
        </radiogroup>
        <button id="ok" text="确定"/>
    </vertical>
);

ui.adroidId.setText("您的序列号为："+device.getAndroidId());
ui.ok.click(function () {
    //停止所有线程
    threads.shutDownAll()

    let tasks = 0
    if (ui.radio1.checked) {
        tasks = 1
    } else if (ui.radio2.checked) {
        tasks = 2
    } else if (ui.radio3.checked) {
        tasks = 3
    } else {
        tasks = 4
    }
    let key = ui.key.getText();
    let sign = 'qa19960401qa'
    let myKey=getMd5(sign+device.getAndroidId())
    // test()
    let time=new Date()
    // toast(time.getFullYear()+'-'+ parseInt(time.getMonth())+'-'+time.getDate())
    if(key==myKey&&(time.getFullYear()+'-'+ parseInt(time.getMonth())+'-'+time.getDate())=='2020-6-8' ){
        switch(tasks){
            case 1:
                threads.start(function(){
                    //在新线程执行的代码
                    goTasks(0)
                });
                break
            case 2:
                threads.start(function(){
                    //在新线程执行的代码
                    goTasks(1)
                });
                break
            case 3:
                threads.start(function(){
                    //在新线程执行的代码
                    goTasks(2)
                });
                break
            case 4:
                threads.start(function(){
                    //在新线程执行的代码
                    goTask()
                });
                break
        }
    }else{
        toast("请输入正确的激活码");
    }
});
// 4b3f87f0fe5a6ba79eb3d1348c228c04
ui.copy.click(function () {
    let sign = 'qa19960401qa'
    toast(getMd5(sign+device.getAndroidId()))
    setClip(getMd5(sign+device.getAndroidId()))
    // setClip(device.getAndroidId());
});
function test(){
    toast("测试函数");
}
function goTask() {
    console.show()
    console.info("20秒后开始")
    sleep(10000)
    console.info("10秒后开始")
    sleep(5000)
    console.info("5秒后开始")
    sleep(1000)
    console.info("4秒后开始")
    sleep(1000)
    console.info("3秒后开始")
    sleep(1000)
    console.info("2秒后开始")
    sleep(1000)
    console.info("1秒后开始")
    sleep(1000)
    console.info("开始")
    var btnParent, btnClose, boundLeft, boundTop, boundRight, boundBottom, wRatio, hRatio, count, total, pointX, pointY, flag;

    //屏幕比用1920X1080的屏幕适配其他
    wRatio = device.width / 1080;
    hRatio = device.height / 1920;

    /**
     * 通过uiautomatorviewer分析获得 2 所在控件 bounds=[219,1194][270,1245]
     * 其中：
     *      boundLeft = 219
     *      boundTop = 1194
     *      boundRight = 270
     *      boundBottom = 1245
     * 适配其他机型 只需横坐标乘以 wRatio  纵坐标乘以 hRatio
     */
    boundLeft = 219 * wRatio;
    boundTop = 1194 * hRatio;
    boundRight = 270 * wRatio;
    boundBottom = 1245 * hRatio;
    /**
     * UiSelector.boundsInside(left, top, right, buttom)
     * 在bound区域内查找控件
     * 在bound区域内查找className("android.view.View") 即 2 所在控件
     */
    let total = 0
    let btn, btn0, btn1, btn2, saveBtn, resetBtn, num,pointX,pointY
    for (let i = 0; i < 9999; i++) {
        console.warn("循环第:" + i + '次')
        btn = findBtnOnce(4);
        if (btn.text() == "高级强化") {
            sleep(200)
            click(btn.bounds().centerX(), btn.bounds().centerY()-10);
            click(btn.bounds().centerX(), btn.bounds().centerY()-15);
            console.log("高级强化已点击!")
            sleep(100)
            btn0 = findNum(0);
            btn1 = findNum(1);
            btn2 = findNum(2);
            console.log("第一个数:" + btn0.text())
            console.log("第二个数:" + btn1.text())
            console.log("第三个数:" + btn2.text())
            num = parseInt(btn0.text()) + parseInt(btn1.text()) + parseInt(btn2.text())
            if (num > 0) {
                console.info("和为正:" + num)
                total = total + num
                saveBtn = findSaveBtn()
                //随机点击坐标
                pointX = saveBtn.bounds().centerX();
                pointY = saveBtn.bounds().centerY();
                // toast(pointX)
                // toast(pointY)
                click(pointX, pointY);
                sleep(500)
                click(pointX, pointY);
            } else {
                console.error("和为负:" + num)
                resetBtn = findResetBtn();
                //随机点击坐标
                // pointX = Math.random()*(boundRight-boundLeft)+boundLeft;
                // pointY = Math.random()*(boundBottom-boundTop)+boundTop;
                click(resetBtn.bounds().centerX(), resetBtn.bounds().centerY());
            }
        } else {
            console.error("未找到高级强化")
        }
        console.warn("已增加:" + total)
        sleep(300)
    }
}
function goTasks(nums){
    console.show()
    toast(nums)
    console.info("点击第"+(nums+1)+"行")
    console.info("20秒后开始")
    sleep(10000)
    console.info("10秒后开始")
    sleep(5000)
    console.info("5秒后开始")
    sleep(1000)
    console.info("4秒后开始")
    sleep(1000)
    console.info("3秒后开始")
    sleep(1000)
    console.info("2秒后开始")
    sleep(1000)
    console.info("1秒后开始")
    sleep(1000)
    console.info("开始")
    var btnParent, btnClose, boundLeft, boundTop, boundRight, boundBottom, wRatio, hRatio, count, total, pointX, pointY, flag;

    //屏幕比用1920X1080的屏幕适配其他
    wRatio = device.width / 1080;
    hRatio = device.height / 1920;

    /**
     * 通过uiautomatorviewer分析获得 2 所在控件 bounds=[219,1194][270,1245]
     * 其中：
     *      boundLeft = 219
     *      boundTop = 1194
     *      boundRight = 270
     *      boundBottom = 1245
     * 适配其他机型 只需横坐标乘以 wRatio  纵坐标乘以 hRatio
     */
    boundLeft = 219 * wRatio;
    boundTop = 1194 * hRatio;
    boundRight = 270 * wRatio;
    boundBottom = 1245 * hRatio;
    /**
     * UiSelector.boundsInside(left, top, right, buttom)
     * 在bound区域内查找控件
     * 在bound区域内查找className("android.view.View") 即 2 所在控件
     */
    let total = 0
    let btn, btn0, saveBtn, resetBtn, num
    for (let i = 0; i < 9999; i++) {
        btn = findBtnOnce(4);
        if (btn.text() == "高级强化") {
            sleep(200)
            click(btn.bounds().centerX(), btn.bounds().centerY());
            console.log("高级强化已点击!")
            sleep(200)
            btn0 = findNum(nums);
            // btn1 = findNum(1);
            // btn2 = findNum(2);
            console.log("第一个数:" + btn0.text())
            // console.log("第二个数:" + btn1.text())
            // console.log("第三个数:" + btn2.text())
            num = parseInt(btn0.text())
            if (num > 0) {
                console.info("正:" + num)
                total = total + num
                saveBtn = findSaveBtn()
                //随机点击坐标
                pointX = saveBtn.bounds().centerX();
                pointY = saveBtn.bounds().centerY();
                // toast(pointX)
                // toast(pointY)
                click(pointX, pointY);
                sleep(500)
                click(pointX, pointY);
            } else {
                console.error("负:" + num)
                resetBtn = findResetBtn();
                //随机点击坐标
                // pointX = Math.random()*(boundRight-boundLeft)+boundLeft;
                // pointY = Math.random()*(boundBottom-boundTop)+boundTop;
                click(resetBtn.bounds().centerX(), resetBtn.bounds().centerY());
            }
        } else {
            console.error("未找到高级强化")
        }
        console.warn("已增加:" + total)
        sleep(500)
    }    
}
function findNum(num) {
    let num0;
    for (let i = 0; i < 10; i++) {
        num0 = id("extendValue").findOnce(num);
        if (num0) {
            break
        }
        sleep(100)
    }
    return num0
}

function findBtnOnce(num) {
    let btn;
    for (let i = 0; i < 10; i++) {
        btn = id("btn").findOnce(num);
        if (btn) {
            break
        }
        sleep(100)
    }
    return btn
}

function findSaveBtn() {
    let btn;
    for (let i = 0; i < 10; i++) {
        btn = id("saveBtn").findOne();
        if (btn) {
            break
        }
        sleep(100)
    }
    return btn
}

function findResetBtn() {
    let btn;
    for (let i = 0; i < 10; i++) {
        btn = id("resetBtn").findOne();
        if (btn) {
            break
        }
        sleep(100)
    }
    return btn
}
// 取md5
function getMd5(string) {
    return java.math.BigInteger(1, java.security.MessageDigest.getInstance("MD5").digest(java.lang.String(string).getBytes())).toString(16);
};